#include <vector>
#include <iostream>
#include <unordered_map>
#include <numeric>
#include <algorithm>
#include <iterator>

using namespace std;

string originalDigits(string s) {
    unordered_map<char, int> c;
    for (char ch: s) {
        ++c[ch];
    }

    vector<int> cnt(10);
    cnt[0] = c['z'];
    cnt[2] = c['w'];
    cnt[4] = c['u'];
    cnt[6] = c['x'];
    cnt[8] = c['g'];

    cnt[3] = c['h'] - cnt[8];
    cnt[5] = c['f'] - cnt[4];
    cnt[7] = c['s'] - cnt[6];

    cnt[1] = c['o'] - cnt[0] - cnt[2] - cnt[4];

    cnt[9] = c['i'] - cnt[5] - cnt[6] - cnt[8];

    string ans;
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < cnt[i]; ++j) {
            ans += char(i + '0');
        }
    }
    return ans;
}

int findInVector(vector<string> nums, string s) {
    for(int i = 0; i < nums.size(); i++){
        if(nums[i] == s) return i;
    }
    return -1;
}

// unordered_map<string, int> wordToNumMap(vector<string> nums) {
//     unordered_map<string, int> ret;
//     for(int i = 0; i < nums.size(); i++){
//         ret.insert({nums[i], i});
//     }
//     return ret;
// }

// 思路有误
string myoriginalDigits(string s) {
    vector<string> nums = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    // string zero = "zero", one = "one", two = "two", three = "three", four = "four", five = "five", six = "six", seven = "seven", eight = "eight", nine = "nine";
    unordered_map<string, unordered_map<char, int>> mp;
    for(string st : nums){
        unordered_map<char, int> chmp;
        for(char ch : st){
            auto r = chmp.insert({ch, 1});
            if(!r.second) ++r.first->second;
        }
        mp.insert({st, chmp});
    }
    for(char ch : s){
        for(auto it = mp.begin(); it != mp.end(); it++){
            if(it->second.find(ch) != it->second.end()) --it->second.find(ch)->second;
        }
    }
    string ret; vector<int> result; // unordered_map<string, int> numsMap = wordToNumMap(nums);
    for(auto it = mp.begin(); it != mp.end(); it++){
        auto i = it->second; int max = INT_MIN;
        for(auto jt = i.begin(); jt != i.end(); jt++){
            if(jt->second > max){
                max = jt->second;
            }
        }
        while(max <= 0){
            result.push_back(findInVector(nums, it->first));
            max++;
        }
        // if(max <= 0) result.push_back(findInVector(nums, it->first)); // ret += (numsMap.find(it->first)->second + '0');
    }
    sort(result.begin(), result.end());
    for(auto i : result){
        ret += i + '0';
    }
    return ret;
}

int main(){
    string s1 = "owoztneoer", s2 = "zerozero";
    cout << originalDigits(s2) << endl;
    return 0;
}